﻿using Microsoft.AspNetCore.Components;
using LaserMark.Components.Events;

namespace LaserMark.Components
{
    public abstract class EventComponentBase : BaseComponent, IAsyncDisposable
    {
        /// <summary>
        /// 
        /// </summary>
        [Inject]
        [NotNull]
        protected IRibbonTabEventBus? EventBus { get; set; }

        /// <summary>
        /// 
        /// </summary>
        protected List<RibbonTabEvent> Events { get; } = new();

        /// <summary>
        /// 处理菜单事件名称集合 添加到此集合的 菜单自动设置 IsActive
        /// </summary>
        protected List<string> EventNames { get; } = new();

        /// <summary>
        /// 
        /// </summary>
        protected List<string> CommandNames { get; } = new();

        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        protected override void OnInitialized()
        {
            base.OnInitialized();
            OnRegisterEvent();
        }

        /// <summary>
        /// 
        /// </summary>
        protected abstract void OnRegisterEvent();


        protected ValueTask DisposeAsync(bool disposing)
        {
            if (disposing)
            {
                foreach (var e in Events)
                {
                    EventBus.UnRegister(e);
                }
            }

            return ValueTask.CompletedTask;
        }

        public async ValueTask DisposeAsync()
        {
            await DisposeAsync(true);
            GC.SuppressFinalize(this);
        }
    }
}